using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ExpertSMS.Models;

namespace ExpertSMS.Controllers
{   
    public class StoreToKitchenEntryItemsController : Controller
    {
        private ExpertSMSContext context = new ExpertSMSContext();


        public ActionResult _StoreToKitchenEntryProductDetails(int foodID)
        {
            ViewBag.PossibleProducts = context.Products;
            return PartialView(foodID);
        }


        [HttpPost]
        public ActionResult _StoreToKitchenEntryProductDetails(int productID, int? unitID, decimal? quantity, int storeToKitchenEntryID)
        {
            Product product = context.Products.Single(x => x.ProductID == productID);
            ViewBag.PossibleProducts = product;
            IEnumerable<Unit> units = product.Units;
            ViewBag.PossibleUnits = units;
            if (unitID.HasValue)
            {
                ViewBag.PossibleUnit = units.FirstOrDefault(x => x.UnitID == unitID.Value);
            }

            return PartialView(storeToKitchenEntryID);
        }
        [HttpPost]
        public ActionResult _StoreToKitchenEntryProductList(int storeToKitchenEntryID)
        {
            return PartialView(context.StoreToKitchenEntries.Single(x => x.StoreToKitchenEntryID == storeToKitchenEntryID).StoreToKitchenEntryItems);
        }
        //
        // GET: /StoreToKitchenEntryItems/

        public ViewResult Index()
        {
            return View(context.StoreToKitchenEntryItems.Include(storetokitchenentryitem => storetokitchenentryitem.StoreToKitchenEntry).ToList());
        }

        //
        // GET: /StoreToKitchenEntryItems/Details/5

        public ViewResult Details(int id)
        {
            StoreToKitchenEntryItem storetokitchenentryitem = context.StoreToKitchenEntryItems.Single(x => x.StoreToKitchenEntryItemID == id);
            return View(storetokitchenentryitem);
        }

        //
        // GET: /StoreToKitchenEntryItems/Create

        public ActionResult Create()
        {
            ViewBag.PossibleStoreToKitchenEntries = context.StoreToKitchenEntries;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View();
        } 

        //
        // POST: /StoreToKitchenEntryItems/Create

        [HttpPost]
        public ActionResult Create(StoreToKitchenEntryItem storetokitchenentryitem)
        {
            if (ModelState.IsValid)
            {

                context.StoreToKitchenEntryItems.Add(storetokitchenentryitem);
                context.SaveChanges();
                Unit unit = context.Units.FirstOrDefault(x => x.UnitID == storetokitchenentryitem.UnitID);
                IEnumerable<ExpertSMS.Models.StoreProductStock> storeProductStocks = context.StoreProductStocks.Where(x=>x.ProductID == storetokitchenentryitem.ProductID);

                decimal quantity = 0;
                decimal cost = 0;
                if (storeProductStocks.Count() > 0)
                {
                    foreach (var stock in context.StoreProductStocks.Where(x => x.ProductID == storetokitchenentryitem.ProductID && x.CurrentQuantity > 0))
                    {
                        decimal costPerUnit = (stock.Cost / stock.OriginalQuantity);
                        cost += (costPerUnit * stock.CurrentQuantity);
                        quantity += stock.CurrentQuantity;
                    }
                }
                storetokitchenentryitem.Cost = (cost / quantity) * (storetokitchenentryitem.Quantity/unit.ConvertingFactorToMainUnit);
                context.SaveChanges();
                context = new ExpertSMSContext();
                return PartialView("_StoreToKitchenEntryProductList", context.StoreToKitchenEntries.Single(x => x.StoreToKitchenEntryID == storetokitchenentryitem.StoreToKitchenEntryID).StoreToKitchenEntryItems);  
            }

            ViewBag.PossibleStoreToKitchenEntries = context.StoreToKitchenEntries;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(storetokitchenentryitem);
        }
        
        //
        // GET: /StoreToKitchenEntryItems/Edit/5
 
        public ActionResult Edit(int id)
        {
            StoreToKitchenEntryItem storetokitchenentryitem = context.StoreToKitchenEntryItems.Single(x => x.StoreToKitchenEntryItemID == id);
            ViewBag.PossibleStoreToKitchenEntries = context.StoreToKitchenEntries;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(storetokitchenentryitem);
        }

        //
        // POST: /StoreToKitchenEntryItems/Edit/5

        [HttpPost]
        public ActionResult Edit(StoreToKitchenEntryItem storetokitchenentryitem)
        {
            if (ModelState.IsValid)
            {
                context.Entry(storetokitchenentryitem).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleStoreToKitchenEntries = context.StoreToKitchenEntries;
            ViewBag.PossibleProducts = context.Products;
            ViewBag.PossibleUnits = context.Units;
            return View(storetokitchenentryitem);
        }

        //
        // GET: /StoreToKitchenEntryItems/Delete/5
 
        public ActionResult Delete(int id)
        {
            StoreToKitchenEntryItem storetokitchenentryitem = context.StoreToKitchenEntryItems.Single(x => x.StoreToKitchenEntryItemID == id);
            return View(storetokitchenentryitem);
        }


        public ActionResult DeleteConfirmed(int id)
        {
            StoreToKitchenEntryItem storetokitchenentryitem = context.StoreToKitchenEntryItems.Single(x => x.StoreToKitchenEntryItemID == id);
            context.StoreToKitchenEntryItems.Remove(storetokitchenentryitem);
            context.SaveChanges();
            context = new ExpertSMSContext();
            return PartialView("_StoreToKitchenEntryProductList", context.StoreToKitchenEntries.Single(x => x.StoreToKitchenEntryID == storetokitchenentryitem.StoreToKitchenEntryID).StoreToKitchenEntryItems);  
        }
    }
}